//
//  TestViewController.m
//  JKOCTextView
//
//  Created by 王冲 on 2018/1/22.
//  Copyright © 2018年 希爱欧科技有限公司. All rights reserved.
//

#import "TestViewController.h"
#import "UIView+JKUiviewExtension.h"

#define CIO_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define CIO_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define TitleBlackCOLOR [UIColor colorWithRed:(51)/255.0 green:(51)/255.0 blue:(51)/255.0 alpha:1]
#define backGayCOLOR [UIColor colorWithRed:(232)/255.0 green:(232)/255.0 blue:(232)/255.0 alpha:1]

@interface TestViewController ()<UITextViewDelegate>

/**
    UITextView输入框的创建
 */
@property(nonatomic,strong) UITextView *jkTextView;
/**
    提示内容
 */
@property(nonatomic,strong) UILabel *placeholderLabel;
/**
    键盘上添加一个view
 */
@property(nonatomic,strong) UIView *tooBarView;
@property(nonatomic,strong) UIButton *tooBarViewBtn;

// 自定义键盘
@property(nonatomic,strong) UIView *keyBoardView;

@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    [self.view addSubview:self.jkTextView];
    [self.jkTextView addSubview:self.placeholderLabel];
    [self.view addSubview: self.tooBarView];
    [self.tooBarView addSubview:self.tooBarViewBtn];
}

#pragma mark 切换键盘
-(void)switchKeyBoard{
    
    // 1.关闭键盘
    [self.jkTextView resignFirstResponder];
    
    // 2.设置inputView
    self.jkTextView.inputView = (self.jkTextView.inputView == nil) ? self.keyBoardView : nil;
    
    // 3.从新召唤出键盘
    [self.jkTextView becomeFirstResponder];
}

#pragma mark 监听键盘
//实现接收到通知时的操作
-(void)keyBoardChange:(NSNotification *)notification
{
    //获取键盘弹出或收回时frame
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    //获取键盘弹出所需时长
    float duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    NSLog(@"键盘的弹出的时间=%lf",duration);
    
    /*
     工具条回弹是因为执行了两次动画, 而系统自带的键盘的动画节奏(曲线) 7
     7在apple API中并没有提供给我们, 但是我们可以使用
     7这种节奏有一个特点: 如果连续执行两次动画, 不管上一次有没有执行完毕, 都会立刻执行下一次
     也就是说上一次可能会被忽略
     
     如果将动画节奏设置为7, 那么动画的时长无论如何都会自动修改为0.5
     
     UIView动画的本质是核心动画, 所以可以给核心动画设置动画节奏
     */
    // 1.取出键盘的动画节奏
    float curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] floatValue];
    
    [UIView animateWithDuration:duration animations:^{
        
        // 2.设置动画节奏
        [UIView setAnimationCurve:curve];
        
        self.tooBarView.y = keyboardFrame.origin.y - 64 - 44;
        
    }];
}


#pragma mark  textview的变化
-(void)textViewDidChange:(UITextView *)textView{
    
    self.placeholderLabel.hidden = textView.hasText;
}

-(UITextView *)jkTextView{
    
    if (!_jkTextView) {
        
        _jkTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, CIO_SCREEN_WIDTH,CIO_SCREEN_HEIGHT-64)];
        [_jkTextView becomeFirstResponder];
        // UITextView可以垂直滑动弹出键盘
        _jkTextView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _jkTextView.alwaysBounceVertical = YES;
        _jkTextView.delegate = self;
        _jkTextView.backgroundColor = [UIColor whiteColor];
        _jkTextView.textColor = TitleBlackCOLOR;
        _jkTextView.font = [UIFont systemFontOfSize:16.f];
    }
    
    return _jkTextView;
}

-(UILabel *)placeholderLabel{
    
    if (!_placeholderLabel) {
        
        _placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 9, CIO_SCREEN_WIDTH-10, 22)];
        _placeholderLabel.text = @"分享新鲜事";
        _placeholderLabel.textColor = backGayCOLOR;
        _placeholderLabel.font = [UIFont systemFontOfSize:20];
    }
    
    return _placeholderLabel;
}

-(UIView *)tooBarView{
    
    if (!_tooBarView) {
    
        _tooBarView = [[UIView alloc]initWithFrame:CGRectMake(0, CIO_SCREEN_HEIGHT-64-44, CIO_SCREEN_WIDTH, 44)];
        _tooBarView.backgroundColor = [UIColor redColor];
    }
    return _tooBarView;
}

-(UIButton *)tooBarViewBtn{
    
    if (!_tooBarViewBtn) {
        
        _tooBarViewBtn = [[UIButton alloc]initWithFrame:CGRectMake(CIO_SCREEN_WIDTH-150, 0, 100, 44)];
        [_tooBarViewBtn addTarget:self action:@selector(switchKeyBoard) forControlEvents:UIControlEventTouchUpInside];
        [_tooBarViewBtn setTitle:@"切换键盘" forState:UIControlStateNormal];
        _tooBarViewBtn.backgroundColor = [UIColor purpleColor];
    }
    return _tooBarViewBtn;
}

-(UIView *)keyBoardView{
    
    if (!_keyBoardView) {
        
        _keyBoardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CIO_SCREEN_WIDTH, 100)];
        _keyBoardView.backgroundColor = [UIColor brownColor];
    }
    
    return _keyBoardView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
